/**
 * 
 */
package com.wegax.finprod.wizardform;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vaadin.teemu.wizards.Wizard;
import org.vaadin.teemu.wizards.event.WizardCancelledEvent;
import org.vaadin.teemu.wizards.event.WizardCompletedEvent;
import org.vaadin.teemu.wizards.event.WizardProgressListener;
import org.vaadin.teemu.wizards.event.WizardStepActivationEvent;
import org.vaadin.teemu.wizards.event.WizardStepSetChangedEvent;

import com.vaadin.data.Validator.InvalidValueException;
import com.wegax.finprod.application.Application;

/**
 * @author wegax
 * 
 */
public abstract class WizardForm<T extends Application> extends Wizard implements WizardProgressListener {

	private static final Logger log = LoggerFactory.getLogger(WizardForm.class);

	public WizardForm() {
		super();
		resultMapper = createResultMapper();
		addListener(this);
	}

	private T resultMapper;

	@Override
	public void next() {
		if (currentStep instanceof WizardFormStep<?>) {
			WizardFormStep<T> wizardFormStep = (WizardFormStep<T>) currentStep;
			try {
				wizardFormStep.getForm().commit();
			} catch (InvalidValueException e) {
				log.debug("Validation error: [{}]", e.getMessage());
				return;
			}

			wizardFormStep.map(resultMapper);
		}
		super.next();
	}

	@Override
	public void finish() {
		if (currentStep instanceof WizardFormStep<?>) {
			WizardFormStep<T> wizardFormStep = (WizardFormStep<T>) currentStep;
			try {
				wizardFormStep.getForm().commit();
			} catch (InvalidValueException e) {
				log.debug("Validation error: [{}]", e.getMessage());
				return;
			}

			wizardFormStep.map(resultMapper);
		}
		log.info("The application is already here: [{}]", resultMapper);
		// TODO (wegax) wniosek trzeba przekazac dalej
		super.finish();
	}

	public void addFormStep(WizardFormStep<T> wizardFormStep) {
		addStep(wizardFormStep);
		wizardFormStep.initFormStep(this);
	}

	public T getResultMapper() {
		return resultMapper;
	}

	protected abstract T createResultMapper();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.event.WizardProgressListener#activeStepChanged(org.vaadin.teemu.wizards.event.
	 * WizardStepActivationEvent)
	 */
	@Override
	final public void activeStepChanged(WizardStepActivationEvent event) {
		if (currentStep instanceof WizardFormStep<?>) {
			((WizardFormStep<T>) currentStep).onShow();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.event.WizardProgressListener#stepSetChanged(org.vaadin.teemu.wizards.event.
	 * WizardStepSetChangedEvent)
	 */
	@Override
	public void stepSetChanged(WizardStepSetChangedEvent event) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.event.WizardProgressListener#wizardCompleted(org.vaadin.teemu.wizards.event.
	 * WizardCompletedEvent)
	 */
	@Override
	public void wizardCompleted(WizardCompletedEvent event) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.event.WizardProgressListener#wizardCancelled(org.vaadin.teemu.wizards.event.
	 * WizardCancelledEvent)
	 */
	@Override
	public void wizardCancelled(WizardCancelledEvent event) {

	}

}
